package com.wang.API;

import com.wang.bean.*;
import com.wang.mapper.SysUserMapper;
import com.wang.service.SinglechoiceResultsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 练题类
 */
@RestController
@RequestMapping("api")
public class SinglechoiceResultsController {

    @Autowired
    private SinglechoiceResultsService singlechoiceResultsService;

    @RequestMapping(value = "/submitsinglechoiceresults")
    @RequiresPermissions("sys:user:submitsinglechoiceresults")
    public Result submitsinglechoiceresults(@RequestBody SinglechoiceResults sr){
        Result result = new Result();

        SinglechoiceSubmissionLog sLog = new SinglechoiceSubmissionLog();
        sLog.setSinglechoice_id(sr.getSinglechoice_id());
        sLog.setUser_id(sr.getUser_id());
        sLog.setUser_option(sr.getUser_option());
        sLog.setSubmissiontime(new Date());
        //singlechoiceResultsService.addsinglechoicesubmissionlog(sLog);
        try{
            if (sr.getUser_option() == null || "".equals(sr.getUser_option())){
                result.setCode("803");
                sLog.setCode("803");
                sLog.setMessage(result.getMessage());
                singlechoiceResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }
            if (sr.getUser_id() == null || sr.getUser_id() == 0 || sr.getSinglechoice_id() == null || sr.getSinglechoice_id() == 0){
                result.setCode("804");
                sLog.setCode("804");
                sLog.setMessage(result.getMessage());
                singlechoiceResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }
            if (singlechoiceResultsService.selectoneSinglechoice(sr.getSinglechoice_id()) == null){
                result.setCode("805");
                sLog.setCode("805");
                sLog.setMessage(result.getMessage());
                singlechoiceResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }
            if (singlechoiceResultsService.selectOneById(sr.getUser_id()) == null){
                result.setCode("806");
                sLog.setCode("806");
                sLog.setMessage(result.getMessage());
                singlechoiceResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }
            if (singlechoiceResultsService.findSinglechoiceResultsByUserSinglechoiceId(sr) != null){
                singlechoiceResultsService.updateSinglechoiceResults(sr);
                result.setCode("802");
                sLog.setCode("802");
                sLog.setMessage(result.getMessage());
                singlechoiceResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            } else {
                singlechoiceResultsService.addSinglechoiceResults(sr);
                result.setCode("801");
                sLog.setCode("801");
                sLog.setMessage(result.getMessage());
                singlechoiceResultsService.addsinglechoicesubmissionlog(sLog);
                return result;
            }
        } catch (Exception e){
            System.out.println(e);
            result.setCode("800");
            sLog.setCode("800");
            sLog.setMessage(result.getMessage());
            singlechoiceResultsService.addsinglechoicesubmissionlog(sLog);
            return result;
        }
    }

}
